$(function () {
  layui.use('laydate', function () {
    var laydate = layui.laydate;

    //执行一个laydate实例
    laydate.render({
      elem: '#rili' //指定元素
    });
  });
// textarea
  tinymce.init({
    selector: '#tinydemo', //容器，可使用css选择器
    language: 'zh_CN', //调用放在langs文件夹内的语言包
  });
  // 下拉框获取文章类别
  $.ajax({
    url: '/admin/category/list',
    headers: { Authorization: localStorage.getItem('token') },
    success: res => {
      // console.log(res)
      let HtmlStr = template('temp_list', res)
      $('.category').html(HtmlStr)
    }
  })
    // 图片预览效果
    $('#inputCover').on('change', function () {
      this.files[0]
      let url = URL.createObjectURL(file)
      $('.article_cover').attr('src', url)
    })
  
  $('.btn-release,.btn-draft').on('click', function (e) {
    e.preventDefault()
    let fd = new FormData(document.querySelector('form'))
    let content = tinyMCE.editors['tinydemo'].getContent()
    fd.append("content", content)
    if ($(this).text() == '发布') {
      fd.append('state', '已发布')
    } else {
      fd.append('state', '')
    }
    $.ajax({
      type: 'post',
      url: '/admin/article/publish',
      data: fd,
      contentType: false,
      processData: false,
      success: res => {
        // console.log(res)
        layer.msg(res.msg, { time: 1000 })
        setTimeout(function () {
          if (res.code == 200) {
            window.location.href = './article_list.html'
          }
        },2000)
      }
    })
    // fd.forEach((value, name) => {
    //   console.log(name, value);
    // })
  })

})